package g1.thermostat;

import android.app.Application;
import android.util.Log;

import java.util.Arrays;

public class DayNightTimes extends Application {
	/*  Custom Class.
	 *  Creates lists of day and night.
	 *  Time format is hhmm. For example; 12:00 hour = 1200, 9:30 hour = 0930.
	 */
	
	int[] day;
	int[] night;
	int dayChanges;
	int nightChanges;
		
	DayNightTimes() {
		
		/* Constructor Lists.
		 * Creates new lists for a day.
		 */
		
		day = new int[] {9999, 9999, 9999, 9999, 9999, 10101};
		night = new int[] {0 , 9999, 9999, 9999, 9999, 9999};
		dayChanges = 0;
		nightChanges = 0;
	}
	
	boolean checkDouble(int time) {
		/* Checks if the time is double.
		 * Returns true/false.
		 */
		
		boolean foundDouble = false;
		
		for (int i=0;i<day.length;i++) {
			if (time == day[i]) {
				foundDouble = true;
			}
		}
		
		for (int i=0;i<night.length;i++) {
			if (time == night[i]) {
				foundDouble = true;
			}
		}
		
		return foundDouble;
	}
	
	int checkDayChange(String name) {
		/* Checks if Day-Night/Night-Day transition is redundant.
		 * Returns redundant time or 9999 if not found.
		 */
		
		int redundantChange = 9999;
		
		if (name.equals("day")) {
			for (int j=1;j<day.length;j++) {
				if (day[j-1] < day[j] && night[j] > day[j]) {
					redundantChange = day[j];
					break;
				}
			}
		}
		
		else if (name.equals("night")) {
			for (int j=1;j<night.length;j++) {
				if (night[j-1] < night[j] && day[j-1] > night[j]) {
					redundantChange = night[j];
					break;
				}
			}
		}
		return redundantChange;
	}
	
	void updateChanges() {
		/* Updates the Changes counter.
		 */
		
		int counterDay = 0;
		int counterNight = -1;
		
		for (int o: day) {
			if (o < 2400) {
				counterDay++;
			}
		}
		
		for (int oi: night) {
			if (oi < 2400) {
				counterNight++;
			}
		}
		dayChanges = counterDay;
		nightChanges = counterNight;
	}
	
	void addTime(int[] list, int time) {
		/* Adds time to specific list.
		 */
		
		for (int i=0;i<list.length;i++) {
			if (list[i] == 9999) {
				list[i] = time;
				i = list.length;
			}
		}
		
		Arrays.sort(list);
	}
	
	void delTime(int[] list, int time) {
		/*  Deletes time from list.
		 */
		
		for (int i=1;i<7;i++) {
			if (list[i] == time) {
				list[i] = 9999;
				i = list.length;
			}
		}
		
		Arrays.sort(list);
	}
	
	int getNextTime(int[] list, int time) {
		int returnValue = 9999;
		for (int i=0;i<list.length;i++) {
			if (list[i] > time && list[i] < 2400) {
				Log.i("TimeCheck",""+list[i]);
				returnValue = list[i];
				i = list.length;
			}
		}
		return returnValue;
	}
	
	int getNextFirst(int[] list, int time) {
		int returnValue = 9999;
		for (int i=0;i<list.length;i++) {
			if (list[i] < 2400) {
				returnValue = list[i];
				i = list.length;
			}
		}
		return returnValue;
	}
}